package com.antdesignbackend.backend.common;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;

public class PathUtil {
    private PathUtil() {
    }

    public static String getApplicationPath() {
        return SystemUtil.getApplicationPath();
    }

    public static String decodeUrl(String url) {
        String result = url;

        try {
            result = URLDecoder.decode(result, "UTF-8");
        } catch (UnsupportedEncodingException var3) {
        }

        return result;
    }

    public static String encodeUrl(String url) {
        String result = url;

        try {
            result = URLEncoder.encode(result, "UTF-8");
        } catch (UnsupportedEncodingException var3) {
        }

        return result;
    }

    public static String getRootPath(String sPath) {
        switch (SystemUtil.getOSType()) {
            case 1:
                return sPath.substring(0, sPath.indexOf(File.separator));
            case 2:
            case 3:
                return "/";
            default:
                return "/";
        }
    }


    public static String trim(String path) {
        return trim(path, false);
    }

    public static String trim(String path, boolean flag) {
        String result = path;
        if (flag && (path.startsWith("\\") || path.startsWith("/"))) {
            result = path.substring(1);
        }

        if (result.endsWith("\\") || result.endsWith("/")) {
            result = result.substring(0, result.length() - 1);
        }

        return result;
    }

    public static String join(String parentPath, String childPath) {
        String result = "";
        result = trim(parentPath) + File.separator + trim(childPath, true);
        return result;
    }

    public static String getParentPath(String path) {
        return (new File(path)).getParent();
    }

    public static String getFileName(String path) {
        String result = trim(path);
        String pattern = "/";
        int a = result.lastIndexOf(File.separator);
        int b = result.lastIndexOf(pattern);
        int index;
        if (a > b) {
            index = a;
        } else {
            index = b;
        }

        return result.substring(index + 1, result.lastIndexOf("."));
    }

    public static String getExtension(String path) {
        String result = null;
        int index = path.lastIndexOf(".");
        if (index > -1) {
            result = path.substring(index);
        }

        return result;
    }

    public static boolean isValid(String path, List<String> filters) {
        boolean result = false;
        String extension = getExtension(path);
        Iterator var4 = filters.iterator();

        while (var4.hasNext()) {
            String filter = (String) var4.next();
            if (extension.equalsIgnoreCase(filter)) {
                result = true;
                break;
            }
        }

        return result;
    }

}
